﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufItem.h"
#include "GnufColorInputControl.h"

namespace gnuf {
    class QtColorControl;
    class Color;
    class ColorPicker;
    class Info;

    class GNUF_BASE_EXPORT ColorControl final 
        : public ItemWith<QtColorControl>
        , public ColorControlAdpatation
    {
        CANNOT_COPY_OR_ASSIGN(ColorControl);
        GNUF_WATCHABLE_OBJECT(ColorControl)
    public:
        static const EItemType Type;
        explicit ColorControl(Item *);
        virtual ~ColorControl();

    public:
        void setColor(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a = 255, bool wantToNotify = false);
        const Color* color() const { return m_color.get(); }

        static std::vector<gnuf::Color> customColors();
        static bool setCustomColors(const std::vector<gnuf::Color>& customColors);

        void enableAlphaChannel(bool enable) { m_enableAlphaChannel = enable; }
        bool isAlphaChannelEnabled() const { return m_enableAlphaChannel; }

        void supportSmartColor(bool support) { m_supportSmartColor = support; }
        bool isSmartColorSupported() { return m_supportSmartColor; }

        void enableSmartColor(bool enable, bool wantToNotify = false);
        bool isSmartColorEnabled() { return m_enableSmartColor; }

        void supportCustomizedInfo(bool support, bool wantToNotify = false);
        bool isCustomizedInfoSupported() const { return m_supportCustomizedInfo; }

        void assignCustomizedInfo(const Info& info);
        const Info * customizedInfo() const;

        void showCustomizedInfoButton(bool show) { m_showCustomizedInfoButton = show; }
        bool isShowCustomizedInfoButton() { return m_showCustomizedInfoButton; }

    protected:
        // Inherits from ItemWith...
        virtual void onDisconnectFromMyQtItem() override;
        virtual void onPostAbilityChanged() override;
        virtual void onPostAppearanceSettingChanged() override;
        virtual void onColorChanged(const Color&) override {}
        virtual void onSmartColorEnabled(bool) override {}
        virtual void onVisibilityChanged(bool enable) override { setVisible(enable); }
        virtual void onAbilityChanged(bool enable) override { setEnabled(enable); }
        virtual void onCollectLocation(std::set<const gcmp::UniqueString *> & locations) const override {
            if (const Item * item = root()) {
                locations.insert(item->uniqueKey());
            }
        }
        virtual void onTableRowSelectionChanged(bool) override;

    private:
        gcmp::OwnerPtr<Color> m_color;
        bool m_enableAlphaChannel;
        bool m_supportSmartColor;
        bool m_enableSmartColor;

        // support customized text and icon
        bool m_supportCustomizedInfo = false;
        gcmp::OwnerPtr<Info> m_opCustomizedInfo = nullptr;
        bool m_showCustomizedInfoButton = false;
    };
    
}
